package com.masterclient.app.businessmodule.homemodule.view.adapter.top;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.masterclient.app.R;
import com.umeng.analytics.MobclickAgent;

public class HomeTopComponent extends LinearLayout implements View.OnClickListener {

    private Context mContext;
    //二维码扫描
//    private RelativeLayout mScanQRView;
//    private ImageView mQrImageView;
    //搜索
    private RelativeLayout mSearchView;
    private ImageView mSearchImageView;
    private TextView mSearchTextView;
    //签到
//    private ImageView mSignImageView;
    //消息
//    private RelativeLayout mMessageView;
//    private ImageView mMessageImageView;
//    private ImageView mRedImageView;

    private TranslateAnimation mShowAction;

    private HomeTopComponentListener iHomeTopComponentListener;

    public void setHomeTopComponentListener(HomeTopComponentListener iHomeTopComponentListener) {
        this.iHomeTopComponentListener = iHomeTopComponentListener;
    }

    public HomeTopComponent(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.component_new_home_top, this);
        initUI();
    }

    public HomeTopComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.component_new_home_top, this);
        initUI();
    }

    private void initUI() {
//        mScanQRView = findViewById(R.id.rl_scan_qr);
//        mScanQRView.setOnClickListener(this);
//        mQrImageView = findViewById(R.id.tv_qr);
        mSearchView = findViewById(R.id.rl_search);
        mSearchView.setOnClickListener(this);
        mSearchImageView = findViewById(R.id.iv_search);
        mSearchTextView = findViewById(R.id.tv_search);
//        mSignImageView = findViewById(R.id.iv_sign);
//        mSignImageView.setOnClickListener(this);
//        mMessageView = findViewById(R.id.rl_message);
//        mMessageView.setOnClickListener(this);
//        mMessageImageView = findViewById(R.id.tv_kefu);
//        mRedImageView = findViewById(R.id.iv_unread);

        //动画
        this.mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1f, Animation.RELATIVE_TO_SELF, 0f);
        mShowAction.setDuration(500);
    }

    /**
     * 当前搜索词
     */
    public TextView getSearchTextView() {
        return mSearchTextView;
    }

    /**
     * 刷新搜索词
     */
    public void refreshTopSearchView(String keyword, boolean isAnimation) {
        mSearchTextView.setText("搜索："+keyword);
        if (isAnimation) {
            mSearchTextView.startAnimation(mShowAction);
        }
    }

    /**
     * 消息红点
     */
    public void refreshRedImageState(boolean isHidden) {
//        if (isHidden) {
//            mRedImageView.setVisibility(INVISIBLE);
//        } else {
//            mRedImageView.setVisibility(VISIBLE);
//        }
    }

    /**
     * 根据透明度改变
     */
    public void refreshTopStateByAlpha(float alphaFloat) {
        if (alphaFloat < 150) {
//            mQrImageView.setImageResource(R.mipmap.icon_sao0);
            mSearchView.setBackgroundResource(R.drawable.shape_stroke_cccccc);
//            mMessageImageView.setImageResource(R.mipmap.icon_xiaoxi0);
            mSearchImageView.setImageResource(R.mipmap.icon_sousuo0);
            mSearchTextView.setTextColor(Color.parseColor("#666666"));
        } else {
//            mQrImageView.setImageResource(R.mipmap.icon_sao);
            mSearchView.setBackgroundResource(R.drawable.shape_stroke_cccccc);
//            mMessageImageView.setImageResource(R.mipmap.icon_xiaoxi);
            mSearchImageView.setImageResource(R.mipmap.icon_sousuo);
            mSearchTextView.setTextColor(Color.parseColor("#b3ffffff"));
        }
    }

    //刷新签到信息
    public void refreshSignData(String imgUrl) {
//        if (mContext != null && mSignImageView != null) {
//            ViewTransformUtil.glideImageView(mContext, imgUrl,
//                    mSignImageView, new Transformation[]{new CenterCrop()}, R.mipmap.defaults);
//        }
    }

    //事件
    @Override
    public void onClick(View v) {
//        if (v.getId() == R.id.rl_scan_qr) {
//            //二维码
//            if (iHomeTopComponentListener != null) {
//                iHomeTopComponentListener.homeTopToQrClick();
//            }
//
//        } else
        if (v.getId() == R.id.rl_search) {
            //搜索
            if (mContext != null) {
                MobclickAgent.onEvent(mContext, "click_search");
            }
            if (iHomeTopComponentListener != null) {
                iHomeTopComponentListener.homeTopToSearchClick();
            }

        }
//        else if (v.getId() == R.id.rl_message) {
//            //消息
//            if (iHomeTopComponentListener != null) {
//                iHomeTopComponentListener.homeTopToMessageClick();
//            }
//
//        }
//        else if (v.getId() == R.id.iv_sign) {
//            //签名
//            if (iHomeTopComponentListener != null) {
//                iHomeTopComponentListener.homeTopToSignClick();
//            }
//        }
    }

}
